package com.ren.scale.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ren.scale.entity.ReclScaleClassify;
import com.ren.scale.entity.excel.ScaleClassify;
import com.ren.scale.entity.vo.ReclScaleFirstClassify;
import com.ren.scale.entity.vo.ReclScaleSecondClassify;
import com.ren.scale.excel_listener.ScaleClassifyExcelListener;
import com.ren.scale.mapper.ReclScaleClassifyMapper;
import com.ren.scale.service.ReclScaleClassifyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author RZY
 * @since 2022-07-14
 */
@Service
@Slf4j
public class ReclScaleClassifyServiceImpl extends ServiceImpl<ReclScaleClassifyMapper, ReclScaleClassify> implements ReclScaleClassifyService {

    /**
     * 获取所有分类结果集
     * @return List<ReclScaleFirstClassify>
     */
    @Override
    public List<ReclScaleFirstClassify> getAllClassifies() {

        //所有一级分类
        List<ReclScaleClassify> firstClassifies = this.list(new QueryWrapper<ReclScaleClassify>()
                .eq("parent_id", 0));
        //所有二级分类
        List<ReclScaleClassify> secondClassifies = this.list(new QueryWrapper<ReclScaleClassify>()
                .ne("parent_id", 0));

        //需要返回的封装结果集合
        List<ReclScaleFirstClassify> res = new ArrayList<>();
        firstClassifies.forEach(reclFirstClassify -> {
            ReclScaleFirstClassify firstClassify = new ReclScaleFirstClassify();
            BeanUtils.copyProperties(reclFirstClassify, firstClassify);
            //二级分类集合
            List<ReclScaleSecondClassify> currentSecondClassifies = new ArrayList<>();
            //遍历二级分类
            secondClassifies.forEach(reclSecondClassify -> {
                //一级分类id匹配二级分类的parent_id
                if(reclSecondClassify.getParentId().equals(firstClassify.getId())) {
                    ReclScaleSecondClassify secondClassify = new ReclScaleSecondClassify();
                    BeanUtils.copyProperties(reclSecondClassify, secondClassify);
                    currentSecondClassifies.add(secondClassify);
                }
            });
            //添加结果集
            res.add(firstClassify.setSecondClassifies(currentSecondClassifies));
        });
        return res;
    }

    @Override
    public boolean addClassifyByFile(MultipartFile file) {
        InputStream in;
        try {
            //获取文件流
            in = file.getInputStream();
            //执行读取文件操作
            EasyExcel
                    .read(in, ScaleClassify.class, new ScaleClassifyExcelListener(new ReclScaleClassifyServiceImpl(),
                            getFirstClassifySet(),
                            getSecondClassifySet()))
                    .sheet()
                    .doRead();
        } catch (Exception e) {
            log.info("excel文件读取失败: " + e);
            return false;
        }

        return true;
    }

    @Override
    public HashSet<String> getFirstClassifySet() {
        //查询所有一级分类标题
        List<ReclScaleClassify> firstClassifies = this.list(new QueryWrapper<ReclScaleClassify>().eq("parent_id", 0L).select("title"));
        return (HashSet<String>) firstClassifies.stream().map(ReclScaleClassify::getTitle).collect(Collectors.toSet());
    }

    @Override
    public HashSet<String> getSecondClassifySet() {
        //查询所有二级分类标题
        List<ReclScaleClassify> secondClassifies = this.list(new QueryWrapper<ReclScaleClassify>().ne("parent_id", 0L).select("title"));
        return (HashSet<String>) secondClassifies.stream().map(ReclScaleClassify::getTitle).collect(Collectors.toSet());
    }
}
